package com.metaera.app.ui.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.ihidea.multilinechooselib.MultiLineChooseLayout;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.FeedbackPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.ToastUtils;
import com.metaera.app.utils.validated.EmptyRule;
import com.metaera.app.utils.validated.Validated;

/**
 * 意见反馈
 *
 * TODO: 待优化
 */
public class FeedbackActivity extends BaseActivity {

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_feed_back;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        // find
        EditText editContent = findViewById(R.id.edit_content);
        EditText editContactPerson = findViewById(R.id.edit_contact_person);
        EditText editContact = findViewById(R.id.edit_contact);
        Button buttonSubmit = findViewById(R.id.button_submit);
        MultiLineChooseLayout multiLineChooseLayout = findViewById(R.id.multiLine_choose_layout);

        // init
        ActivityTitleHelper.setTitle(this, R.string.feedback_text, R.string.feedback_history_text);
        multiLineChooseLayout.setList(getResources().getStringArray(R.array.feedback_types));
        Validated.add(buttonSubmit, new EmptyRule(editContent), new EmptyRule(editContactPerson));

        // network
        FeedbackPresenter presenter = new FeedbackPresenter();
        presenter.setModelAndView(new BasicNetworkRequestView<>(data -> finish()));

        // listener
        findViewById(R.id.tv_action).setOnClickListener(v ->
                startActivity(FeedbackHistoryActivity.class));

        buttonSubmit.setOnClickListener(v ->
                {

                    int selectedIndex = multiLineChooseLayout.getSelectedIndex();
                    if (selectedIndex == -1){
                        ToastUtils.makeText("请选择反馈类型");
                        return;
                    }

                    if (TextUtils.isEmpty(editContent.getText())){
                        ToastUtils.makeText("反馈内容不能为空");
                        return;
                    }

                    if (TextUtils.isEmpty(editContactPerson.getText())){
                        ToastUtils.makeText("联系人不能为空");
                        return;
                    }

                    if (TextUtils.isEmpty(editContact.getText())){
                        ToastUtils.makeText("联系方式不能为空");
                        return;
                    }

                    presenter.submit(
                            selectedIndex + 1,
                            editContent.getText(),
                            editContactPerson.getText(),
                            editContact.getText()
                    );
                }
        );

    }

}
